package top.jiangqiang.qyblog.admin.controller;

import cn.hutool.extra.pinyin.PinyinUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;
import top.jiangqiang.qyblog.common.base.controller.BaseController;
import top.jiangqiang.qyblog.common.base.query.PageResult;
import top.jiangqiang.qyblog.common.base.response.Result;
import top.jiangqiang.qyblog.common.constant.RequestURLConstants;
import top.jiangqiang.qyblog.core.domain.dto.ArticleQueryDto;
import top.jiangqiang.qyblog.core.domain.dto.ArticleSaveOrUpdateDto;
import top.jiangqiang.qyblog.core.domain.dto.ArticleSettingDto;
import top.jiangqiang.qyblog.core.domain.vo.ArticleVo;
import top.jiangqiang.qyblog.core.service.IArticleService;

/**
 * @author jiangqiang
 * @date 2022-12-03
 */
@RequiredArgsConstructor
@RestController
@RequestMapping(RequestURLConstants.ARTICLE_CONTROLLER_PREFIX)
@Tag(name = "文章接口")
public class ArticleController extends BaseController {
    private final IArticleService articleService;

    @GetMapping
    @Operation(summary = "根据条件查询文章列表", description = "可以实现的业务场景有：</br>" +
            "1.根据文章标题和文章状态进行筛选" +
            "2.查询指定标签下的文章" +
            "3.查询指定分类下的文章")
    public Result<PageResult<ArticleVo>> list(@ParameterObject ArticleQueryDto articleQueryDto) {
        return articleService.findByCondition(articleQueryDto);
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询文章详情", description = "查询文章详情")
    public Result<ArticleVo> get(@PathVariable Long id) {
        return articleService.getArticleById(id);
    }

    @PostMapping("/saveOrUpdate")
    @Operation(summary = "新增或编辑文章", description = "新增或编辑文章")
    public Result<ArticleVo> saveOrUpdate(@RequestBody ArticleSaveOrUpdateDto articleSaveOrUpdateDto) {
        return articleService.saveOrUpdateArticle(articleSaveOrUpdateDto);
    }

    @PutMapping("/setting")
    @Operation(summary = "修改文章设置", description = "修改文章设置")
    public Result<ArticleVo> setting(@RequestBody ArticleSettingDto articleSettingDto) {
        return articleService.setting(articleSettingDto);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除文章", description = "根据id删除文章，一旦删除不可找回，如果仅仅是不需要被前台页面展示，直接取消发布状态即可")
    @Deprecated
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.ok(articleService.deleteArticleById(id));
    }

}
